package com.linln.admin.pcs.service;

import com.linln.admin.config.IDCardEncryptor;
import com.linln.admin.config.SmsUtils;
import com.linln.admin.pcs.api.SlabApiController;
import com.linln.modules.system.domain.Order;
import com.linln.modules.system.repository.OrderRepository;
import com.linln.modules.system.service.OrderAmountsService;
import com.linln.modules.system.service.OrderPortersService;
import com.linln.modules.system.service.SmsLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author liudong
 * @date 2025/5/10 10:38
 */
@Service
public class OrderTempService {
    private final static Logger logger = LoggerFactory.getLogger(OrderTempService.class);
    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private OrderAmountsService orderAmountsService;

    @Autowired
    private OrderPortersService orderPortersService;

    @Autowired
    private SmsLogService smsLogService;

    /**
     *
     * @param id  订单id
     * @param startDay 开始日期
     */
    public void jifei(long id,String startDay){
        Order order1 = orderRepository.findById(id).get();
        Double price = order1.getPrice();

        //扣除第一次费用
        int stageType = order1.getStageType();
        double total = 0;
        Double total1 = order1.getTotal();
        if (stageType==1){
            BigDecimal bd = new BigDecimal(price);
            BigDecimal td = new BigDecimal(total1);
            total = Double.parseDouble(String.valueOf(td.add(bd)));
        }else{
            BigDecimal bd = new BigDecimal(price);
            BigDecimal bnum = new BigDecimal(order1.getHourNum());
            BigDecimal result = bd.multiply(bnum);
            BigDecimal td = new BigDecimal(total1);
            BigDecimal rest = td.add(result);
            BigDecimal dca = rest.setScale(2, RoundingMode.HALF_UP);
            total = dca.doubleValue();
        }
        order1.setTotal(total);
        order1.setNum(order1.getNum()+1);
        //订单状态 2 服务中
        order1.setOrderStatus(2);
        order1.setStartDay(startDay);

        //消费一次金额变动
        orderAmountsService.updateOrderAmounts(order1,"患者第一次费用扣除",1);
        //更新记录
        String timeStar = "";
        if (order1.getStartDay().contains(":")){
            timeStar = order1.getStartDay();
        }else{
            timeStar = order1.getStartDay()+" "+order1.getTimeStart();
        }

        orderPortersService.updatePorterOrder(order1.getId(),order1.getPorterId(),0L,order1.getCompanyId(),1,timeStar,"",1,order1.getType());
        orderRepository.save(order1);
        String contents = "您有一个新的陪护订单,请进入系统查看详情信息并及时赶往患者病房。";

        try {
            if (StringUtils.isNotEmpty(order1.getPorterPhone())){
                String ss = IDCardEncryptor.aesDecrypt(order1.getPorterPhone());
                String resp = SmsUtils.sendSms(ss, contents);
                logger.error("开始服务消息发送："+resp);
                smsLogService.insert(order1.getSn(),order1.getPorterPhone(),2,2,order1.getPorterId(),contents,resp);
            }else{
                smsLogService.insert(order1.getSn(),order1.getPorterPhone(),2,2,order1.getPorterId(),contents,"发送失败,手机号为空");
            }
        } catch (Exception e) {
            logger.error("开始服务消息发送失败："+e.getMessage());
        }
    }
}
